package cn.schoolwow.ams.test.module.testcase.flow.button;

import cn.schoolwow.ams.domain.option.QuickAMSOption;
import cn.schoolwow.ams.test.entity.TestCase;
import cn.schoolwow.ams.test.module.testcase.domain.TestCaseFlow;
import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dml.CompareInstanceListResult;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.queryPackage.QueryPackage;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ResetTestCaseFlow implements BusinessFlow {

    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String option = flowContext.getData("option", String.class, "reset");
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        QuickAMSOption quickAMSOption = applicationContext.getBean(QuickAMSOption.class);
        DAO starterDAO = applicationContext.getBean("starterDAO", DAO.class);

        List<TestCaseFlow> testCaseFlowList = QueryPackage.newQuery("cn.schoolwow.ams.test")
                .addClassQuery(TestCaseFlow.class)
                .execute()
                .getInstanceList(TestCaseFlow.class);
        if(testCaseFlowList.isEmpty()){
            starterDAO.clear(TestCase.class);
            return;
        }

        List<TestCase> testCaseList = new ArrayList<>();
        for(TestCaseFlow testCaseFlow:testCaseFlowList){
            TestCase testCase = new TestCase();
            testCase.setCategoryName("其他");
            for(Map.Entry<String,String> entry:quickAMSOption.packageNameLabelMap.entrySet()){
                String className = testCaseFlow.getClass().getName();
                if(className.contains(entry.getKey())){
                    testCase.setCategoryName(entry.getValue());
                }
            }
            BusinessFlow businessFlow = testCaseFlow;
            testCase.setName(businessFlow.name());
            testCaseList.add(testCase);
        }

        switch (option){
            case "reset":{
                starterDAO.startTransaction(transaction -> {
                    transaction.clear(TestCase.class);
                    transaction.insert(testCaseList);
                });
            }break;
            case "compare":{
                List<TestCase> testCaseTotalList = starterDAO.query(TestCase.class).execute().getList();
                CompareInstanceListResult compareInstanceListResult = starterDAO.compareInstanceList(testCaseList, testCaseTotalList);
                starterDAO.insert(compareInstanceListResult.insertInstanceList);
                starterDAO.update(compareInstanceListResult.updateInstanceList);
                starterDAO.delete(compareInstanceListResult.deleteInstanceList);
            }break;
        }
    }

    @Override
    public String name() {
        return "重置测试用例表";
    }
}
